package com.note.chh.notebook.UI.enjoy.netMusicFragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.note.chh.notebook.Adapter.RankAdapter;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.Model.NetMusicTypeModel;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseFragment;
import com.note.chh.notebook.UI.enjoy.rank.RankListActivity;
import com.note.chh.notebook.Uitls.ItemDivider;
import com.note.chh.notebook.Uitls.gson.GsonImpl;

import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 * 排行榜
 */
public class RankFragment extends BaseFragment {


    @BindView(R.id.mRecycle_rank)
    RecyclerView mRecycleRank;
    private BaseQuickAdapter rankAdapter;

    public RankFragment() {
        // Required empty public constructor
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_rank;
    }

    @Override
    public void initView(View view) {

        mRecycleRank.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mRecycleRank.addItemDecoration(new ItemDivider().setDividerWith(SizeUtils.dp2px(10)).setDividerColor(getResources().getColor(R.color.colorTransparent)));
        rankAdapter = new RankAdapter(R.layout.item_rank);
        rankAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRecycleRank.setAdapter(rankAdapter);
    }

    @Override
    public void initData() {
        getRankType();
    }

    @Override
    public void initListener() {
        super.initListener();
        rankAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                NetMusicTypeModel.RankBean.ListBean listBean = (NetMusicTypeModel.RankBean.ListBean) adapter.getData().get(position);

                Bundle bundle = new Bundle();
                bundle.putInt("Rankid",listBean.getRankid());
                bundle.putString("rankname",listBean.getRankname());
                bundle.putString("imageUrl",listBean.getBannerurl().replace("{size}",""));
                startActivity(RankListActivity.class,bundle);
            }
        });
    }

    //获取音乐排行榜 类别
    private void getRankType(){
        OkGo.<String>get("http://m.kugou.com/rank/list&json=true")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        NetMusicTypeModel musicTypeModel = GsonImpl.get().toObject(response.body(), NetMusicTypeModel.class);
                        List<NetMusicTypeModel.RankBean.ListBean> list = musicTypeModel.getRank().getList();
                        rankAdapter.setNewData(list);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (!NetworkUtils.isAvailableByPing()) {
                            showToast(Constant.NETWORKNOUSER);
                        }else{
                            showToast(Constant.UNKNOKNERROR);
                        }
                    }
                });
    }

}
